package org.mbcorp.sar.util.mail;

import javax.jms.Message;
import javax.jms.MessageListener;

import org.apache.activemq.command.ActiveMQObjectMessage;
import org.mbcorp.sar.model.Alerta;
import org.springframework.beans.factory.annotation.Autowired;

public class AMQAlertaListener implements MessageListener {

    @Autowired
    AMQMsgSender amqMsgSender;

    public void onMessage(Message message) {

        System.out.println("OnMessage: Listener working... " + message);
        System.out.println("OnMessage: message instance matched = " + (message instanceof ActiveMQObjectMessage));

        if (message instanceof ActiveMQObjectMessage)
        {
            try
            {
                System.out.println("Converting object to AMQAlerta class.");
                Alerta alerta = (Alerta) ((ActiveMQObjectMessage) message).getObject();
                System.out.println("OnMessage: Object received in Listener as " + alerta);

                /* call message sender to put message onto second queue */

                amqMsgSender.sendMessage(alerta);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
}